package com.zh.chat.gui;

import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;

import com.zh.chat.controller.ClientContext;
import com.zh.chat.entity.UserInfo;
import com.zh.chat.gui.datechooser.DateChooserJButton;
import com.zh.chat.img.ImageSizer;
import com.zh.chat.util.FileOperationUtil;
import com.zh.chat.util.FileType;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * 用户详细信息显示框
 * 
 * @author 逍遥の剑仙 2013-3-6
 */
public class UserInfoFrame extends javax.swing.JFrame {

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JButton okBtn; // 确定按钮
	private JLabel sculpture;
	private DateChooserJButton dateChooserBtn; // 选择日期按钮
	private JPanel birthdayPanel; // 生日栏
	private JPanel sexPanel; // 性别选择面板
	private JRadioButton sexFRb; // 性别选择按钮组
	private ButtonGroup sexRB;
	private JRadioButton sexMRb;
	private JTextField lastLoginDateTF; // 上次登录时间JTextField
	private JTextField nameTF; // 姓名JTextField
	private JTextField registDateTF; // 注册时间JTextField
	private JTextField nickNameTF; // 昵称JTextField
	private JTextField sysIdTF; // SIM号JTextField
	private JPanel infoTextPanel;
	private JButton applicateBtn;
	private JPanel btnPanel;
	private JButton cancelBtn;
	private JLabel lastLoginDateLb;
	private JLabel rigisterDateLb;
	private JLabel nameLb;
	private JLabel sexLb;
	private JLabel birthdayLb;
	private JLabel nickNameLb;
	private JLabel sysIdLb;
	private JButton setSculpture;
	// 以下是系统关联对象
	private ClientContext clientContext = null;
	private UserInfo userInfo = null;
	private boolean isNew = false;

	public boolean isNew() {
		return isNew;
	}

	public void setNew(boolean isNew) {
		this.isNew = isNew;
	}

	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}

	public void setUserInfo(UserInfo userInfo) {
		sysIdTF.setText(userInfo.getSysId() + "");
		nickNameTF.setText(userInfo.getNickname());
		nameTF.setText(userInfo.getName());
		String sex = userInfo.getSex();
		if (sex.equals("F")) {
			sexFRb.setSelected(true);
			sexMRb.setSelected(false);
		} else if (sex.equals("M")) {
			sexFRb.setSelected(false);
			sexMRb.setSelected(true);
		}
		dateChooserBtn.setDate(userInfo.getBirthday());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		registDateTF.setText(sdf.format(userInfo.getRegisterDate()));
		lastLoginDateTF.setText(sdf.format(userInfo.getLastLoginDate()));
		// 初始化头像
		String srcPath = FileOperationUtil.getFilePath(FileType.SCULPTURE,
				userInfo.getSysId(), userInfo.getImage());
		sculpture = new JLabel();
		Image image = new BufferedImage(170, 170, BufferedImage.TRANSLUCENT);
		Graphics g = image.getGraphics();
		Image img = null;
		try {
			img = ImageIO.read(new File(srcPath));
		} catch (IOException e) {
			try {
				img = ImageIO.read(UserInfoFrame.class.getClassLoader().getResourceAsStream("com/zh/chat/image/icon/96px.png"));
			} catch (IOException e1) {
			}
		}
		g.drawImage(img, 0, 0, 170, 170, null);
		sculpture.setIcon(new ImageIcon(image));
		getContentPane().add(sculpture);
		sculpture.setBounds(307, 16, 170, 170);
		sculpture.setBorder(new LineBorder(new java.awt.Color(192, 192, 192),
				3, false));
		// 设置userInfo属性
		this.userInfo = userInfo;
	}

	/**
	 * Auto-generated main method to display this JFrame
	 */
	// public static void main(String[] args) {
	// UserInfoFrame frame = new UserInfoFrame();
	// UserInfo userInfo = new UserInfo("zxdgsadg", "大侠破地方", new Date(),
	// new Date(), 1, 2, 21, "192.165.45.1", "head2.png");
	// userInfo.setSysId(2341234L);
	// userInfo.setSex("F");
	// userInfo.setName("新阿什顿飞");
	// userInfo.setBirthday(new Date());
	// frame.setUserInfo(userInfo);
	// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	// frame.setVisible(true);
	// }

	public UserInfoFrame() {
		super();
		initGUI();
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				setVisible(false);
				clientContext.closeUserInfoFrame();
			}
		});
	}

	private void initGUI() {
		try {
			// 设置左上角的图标
			URL url = this.getClass().getClassLoader()
					.getResource("com/zh/chat/image/icon/32px.png");
			setIconImage(Toolkit.getDefaultToolkit().createImage(url));
			{
				getContentPane().setLayout(null);
				getContentPane().setForeground(new java.awt.Color(0, 0, 0));
				getContentPane().setBackground(
						new java.awt.Color(223, 228, 255));
				this.setPreferredSize(new java.awt.Dimension(500, 300));
				this.setLocationRelativeTo(null);
				{
					setSculpture = new JButton();
					getContentPane().add(setSculpture);
					setSculpture.setText("\u8bbe\u7f6e\u5934\u50cf");
					setSculpture.setBounds(353, 200, 81, 24);
					setSculpture.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseClicked(MouseEvent e) {
							// TODO 当点击设置头像按钮时
							JFileChooser chooser = new JFileChooser();
							chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
								@Override
								public String getDescription() {
									return "*.gif,*.jpg,*.jpeg,*.bmp,*.png";
								}

								@Override
								public boolean accept(File file) {
									String filename = file.getName();
									if (filename
											.matches("^\\S+\\.(gif|jpg|jpeg|bmp|png)$")) {
										return true;
									} else if (file.isDirectory()) {
										return true;
									}
									return false;
								}
							});
							chooser.setMultiSelectionEnabled(false);
							File desktop = new File(System.getProperty("user.home") + System.getProperty("file.separator") + "Desktop");
							chooser.setCurrentDirectory(desktop);
							chooser.showOpenDialog(null);
							File selected = chooser.getSelectedFile();
							if (selected != null) {
								String oldPath = FileOperationUtil.getFilePath(
										FileType.SCULPTURE,
										userInfo.getSysId(),
										userInfo.getImage());
								String newPath = setSculpture(selected
										.getAbsolutePath());// 界面上设置头像
								userInfo.setImage(newPath);
								clientContext.modifySculpture(userInfo.getUser(), oldPath);// 修改头像
							}
						}
					});
				}
				{
					btnPanel = new JPanel();
					FlowLayout btnPanelLayout = new FlowLayout(FlowLayout.RIGHT);
					getContentPane().add(btnPanel);
					btnPanel.setBounds(0, 240, 492, 35);
					btnPanel.setBackground(new java.awt.Color(136, 199, 251));
					btnPanel.setBorder(new LineBorder(new java.awt.Color(147,
							234, 255), 1, false));
					btnPanel.setLayout(btnPanelLayout);
					{
						okBtn = new JButton();
						btnPanel.add(okBtn);
						okBtn.setText("\u786e\u5b9a");
						okBtn.setBounds(228, 199, 71, 23);
						okBtn.setPreferredSize(new java.awt.Dimension(76, 23));
						okBtn.addMouseListener(new MouseAdapter() {
							@Override
							public void mouseClicked(MouseEvent e) {
								if (!isNew) {
									userInfo.setBirthday(dateChooserBtn
											.getDate());
									userInfo.setName(nameTF.getText());
									userInfo.setNickname(nickNameTF.getText());
									if (sexFRb.isSelected()) {
										userInfo.setSex("F");
									} else if (sexMRb.isSelected()) {
										userInfo.setSex("M");
									}
									clientContext.modifyUser(userInfo);
								}
								UserInfoFrame.this.setVisible(false);
								clientContext.closeUserInfoFrame();
							}
						});
					}
					{
						cancelBtn = new JButton();
						btnPanel.add(cancelBtn);
						cancelBtn.setText("\u53d6\u6d88");
						cancelBtn.setBounds(316, 199, 71, 23);
						cancelBtn.setPreferredSize(new java.awt.Dimension(76,
								23));
						cancelBtn.addMouseListener(new MouseAdapter() {
							// TODO 点击取消按钮关闭窗口什么都不做
							@Override
							public void mouseClicked(MouseEvent e) {
								UserInfoFrame.this.setVisible(false);
								clientContext.closeUserInfoFrame();
							}
						});
					}
					{
						applicateBtn = new JButton();
						btnPanel.add(applicateBtn);
						applicateBtn.setVerticalTextPosition(JButton.CENTER);
						applicateBtn.setHorizontalTextPosition(JButton.CENTER);
						applicateBtn.setText("\u5e94\u7528");
						applicateBtn.setBounds(405, 199, 71, 23);
						applicateBtn.setPreferredSize(new java.awt.Dimension(
								76, 23));
						applicateBtn.setForeground(new java.awt.Color(0, 0, 0));
						applicateBtn.setBackground(new java.awt.Color(147, 234,
								255));
						applicateBtn.addMouseListener(new MouseAdapter() {
							// TODO 点击应用按钮后保存UserInfo设置
							@Override
							public void mouseClicked(MouseEvent e) {
								userInfo.setBirthday(dateChooserBtn.getDate());
								userInfo.setName(nameTF.getText());
								userInfo.setNickname(nickNameTF.getText());
								if (sexFRb.isSelected()) {
									userInfo.setSex("F");
								} else if (sexMRb.isSelected()) {
									userInfo.setSex("M");
								}
								clientContext.modifyUser(userInfo);
							}
						});
					}
				}
				{
					infoTextPanel = new JPanel();
					GridBagLayout infoTextPanelLayout = new GridBagLayout();
					infoTextPanelLayout.columnWidths = new int[] { 93, 7 };
					infoTextPanelLayout.rowHeights = new int[] { 10, 10, 10,
							10, 10, 10, 10 };
					infoTextPanelLayout.columnWeights = new double[] { 0.0, 0.1 };
					infoTextPanelLayout.rowWeights = new double[] { 0.1, 0.1,
							0.1, 0.1, 0.1, 0.1, 0.1 };
					getContentPane().add(infoTextPanel);
					infoTextPanel.setLayout(infoTextPanelLayout);
					infoTextPanel.setBounds(2, 0, 279, 240);
					infoTextPanel.setOpaque(false);
					{
						sysIdLb = new JLabel();
						infoTextPanel.add(sysIdLb, new GridBagConstraints(0, 0,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						sysIdLb.setText("SIM\u53f7");
						sysIdLb.setBounds(305, 10, 48, 15);
					}
					{
						nickNameLb = new JLabel();
						infoTextPanel.add(nickNameLb, new GridBagConstraints(0,
								1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						nickNameLb.setText("\u6635\u79f0");
						nickNameLb.setBounds(305, 35, 48, 15);
					}
					{
						nameLb = new JLabel();
						infoTextPanel.add(nameLb, new GridBagConstraints(0, 2,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						nameLb.setText("\u59d3\u540d");
						nameLb.setBounds(305, 60, 48, 15);
					}
					{
						sexLb = new JLabel();
						infoTextPanel.add(sexLb, new GridBagConstraints(0, 3,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						sexLb.setText("\u6027\u522b");
						sexLb.setBounds(305, 86, 48, 15);
					}
					{
						birthdayLb = new JLabel();
						infoTextPanel.add(birthdayLb, new GridBagConstraints(0,
								4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.NONE,
								new Insets(0, 0, 0, 0), 0, 0));
						birthdayLb.setText("\u751f\u65e5");
						birthdayLb.setBounds(305, 111, 48, 15);
					}
					{
						rigisterDateLb = new JLabel();
						infoTextPanel.add(rigisterDateLb,
								new GridBagConstraints(0, 5, 1, 1, 0.0, 0.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.NONE, new Insets(0,
												0, 0, 0), 0, 0));
						rigisterDateLb.setText("\u6ce8\u518c\u65f6\u95f4");
						rigisterDateLb.setBounds(305, 136, 48, 15);
					}
					{
						lastLoginDateLb = new JLabel();
						infoTextPanel.add(lastLoginDateLb,
								new GridBagConstraints(0, 6, 1, 1, 0.0, 0.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.NONE, new Insets(0,
												0, 0, 0), 0, 0));
						lastLoginDateLb.setText("\u4e0a\u6b21\u767b\u5f55");
						lastLoginDateLb.setBounds(305, 162, 48, 15);
					}
					{
						sysIdTF = new JTextField();
						infoTextPanel.add(sysIdTF, new GridBagConstraints(1, 0,
								1, 1, 0.0, 0.0, GridBagConstraints.WEST,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
						sysIdTF.setEditable(false);
					}
					{
						nickNameTF = new JTextField();
						infoTextPanel.add(nickNameTF, new GridBagConstraints(1,
								1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
					}
					{
						nameTF = new JTextField();
						infoTextPanel.add(nameTF, new GridBagConstraints(1, 2,
								1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
					}
					{
						registDateTF = new JTextField();
						infoTextPanel.add(registDateTF, new GridBagConstraints(
								1, 5, 1, 1, 0.0, 0.0,
								GridBagConstraints.CENTER,
								GridBagConstraints.HORIZONTAL, new Insets(0, 0,
										0, 0), 0, 0));
						registDateTF.setEditable(false);
					}
					{
						lastLoginDateTF = new JTextField();
						infoTextPanel.add(lastLoginDateTF,
								new GridBagConstraints(1, 6, 1, 1, 0.0, 0.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.HORIZONTAL,
										new Insets(0, 0, 0, 0), 0, 0));
						lastLoginDateTF.setEditable(false);
					}
					{
						sexPanel = new JPanel();
						infoTextPanel.add(sexPanel, new GridBagConstraints(1,
								3, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
								GridBagConstraints.BOTH,
								new Insets(0, 0, 0, 0), 0, 0));
						sexPanel.setLayout(null);
						sexRB = new ButtonGroup();
						sexPanel.setSize(193, 20);
						sexPanel.setOpaque(false);
						{
							sexFRb = new JRadioButton();
							sexPanel.add(sexFRb);
							sexRB.add(sexFRb);
							sexFRb.setText("\u5973");
							sexFRb.setBounds(0, 0, 50, 20);
							sexFRb.setOpaque(false);

						}
						{
							sexMRb = new JRadioButton();
							sexPanel.add(sexMRb);
							sexRB.add(sexMRb);
							sexMRb.setText("\u7537");
							sexMRb.setBounds(47, 0, 50, 20);
							sexMRb.setOpaque(false);
						}
					}
					{
						birthdayPanel = new JPanel();
						infoTextPanel.add(birthdayPanel,
								new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0,
										GridBagConstraints.CENTER,
										GridBagConstraints.BOTH, new Insets(0,
												0, 0, 0), 0, 0));
						birthdayPanel.setLayout(null);
						birthdayPanel.setOpaque(false);
						{
							dateChooserBtn = new DateChooserJButton();
							birthdayPanel.add(dateChooserBtn);
							dateChooserBtn.setDate(new Date());
							dateChooserBtn.setBounds(0, 0, 186, 30);
						}
					}
				}
			}
			this.setSize(503, 313);
			this.setResizable(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String setSculpture(String srcPath) {
		Image image = new BufferedImage(170, 170, BufferedImage.TRANSLUCENT);
		Graphics g = image.getGraphics();
		int imageName = 0;
		try {
			imageName = (int) (userInfo.getNickname().hashCode() + System
					.currentTimeMillis());
			String path = FileOperationUtil.getFilePath(FileType.SCULPTURE,
					userInfo.getSysId(), imageName + ".jpg");
			File saveFile = new File(path);
			if (!saveFile.getParentFile().exists()) {
				saveFile.getParentFile().mkdirs();
			}
			ImageSizer.saveImageAsJpg(srcPath, path, 170, 170);
			BufferedImage img = ImageIO.read(new File(path));
			g.drawImage(img, 0, 0, 170, 170, null);
			sculpture.setIcon(new ImageIcon(image));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return imageName + ".jpg";
	}
}
